package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CategoryBrandServiceImpl
 * @Author 波
 * @Date 2024/7/29 19:03
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Resource
    private CategoryBrandMapper categoryBrandMapper;
    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询分类品牌列表
     *
     * @param categoryBrand 分页查询的条件
     * @return CategoryBrand列表
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {

        return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
    }

    /**
     * 根据分类品牌id查询分类品牌详细信息
     *
     * @param id 传入的分类品牌id
     * @return CategoryBrand
     */
    @Override
    public CategoryBrand selectCategoryBrandById(Integer id) {
        CategoryBrand categoryBrand = this.getById(id);
        List<Long> categoryList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryList);
        return categoryBrand;
    }
    /**
     * 新增分类品牌信息
     * @param categoryBrand 传入的categoryBrand对象
     * @return 受影响的行数
     */
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        long count=categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if(count > 0) {
            throw new ServiceException("该分类已加该品牌");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }
    /**
     * 修改分类品牌信息
     * @param categoryBrand 传入的categoryBrand对象
     * @return 受影响的行数
     */
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if(count > 0) {
            throw new ServiceException("该分类已加该品牌");
        }

        return categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 根据categoryId查询Brand列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> getBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.getBrandListByCategoryId(categoryId);
    }
}
